package com.xbongbong.paas.script.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.pojo.dto.FixDataJobDTO;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.redis.RedisTimeConstant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.config.ProBaseConfig;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.enums.CompanyConfigEnum;
import com.xbongbong.sys.domain.entity.CompanyConfigEntity;
import com.xbongbong.sys.model.CompanyConfigModel;
import com.xbongbong.workflow.UpgradeService;
import com.xbongbong.workflow.pojo.UpgradeToWorkflowDTO;
import com.xbongbong.workflow.upgrade.BusinessRuleUpgradeWorkflowHelp;
import com.xbongbong.workflow.upgrade.WorkflowDataUpgradeHelp;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2021/06/09 15:55
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/businessRule/upgrade")
public class WorkflowUpgradeController {

    @Resource
    private UpgradeService upgradeService;
    @Resource
    private PaasRedisHelper paasRedisHelper;
    @Resource
    private CompanyConfigModel companyConfigModel;
    @Resource
    private WorkflowDataUpgradeHelp workflowDataUpgradeHelp;
    @Resource
    private BusinessRuleUpgradeWorkflowHelp businessRuleUpgradeWorkflowHelp;
    @Resource
    private ProBaseConfig proBaseConfig;

    @RequestMapping(value = "/workflow", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid UpgradeToWorkflowDTO upgradeToWorkflowDTO, BindingResult br) throws XbbException {
        XbbResponse<BaseVO> response;
        if (!proBaseConfig.getXbbCode().equals(upgradeToWorkflowDTO.getXbbCode())) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
        }
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
               return upgradeService.upgradeBusinessRule(upgradeToWorkflowDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/workflowBack", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String workflowBack(@RequestBody @Valid FixDataJobDTO fixDataJobDTO, BindingResult br) throws XbbException {
        XbbResponse<BaseVO> response;
        if (!proBaseConfig.getXbbCode().equals(fixDataJobDTO.getXbbCode())) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
        }
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                businessRuleUpgradeWorkflowHelp.workflowBack(fixDataJobDTO);
                response = new XbbResponse<>();
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 迁移工作流数据部分
     * @param fixDataJobDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/workflowData", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String workflowData(@RequestBody @Valid FixDataJobDTO fixDataJobDTO, BindingResult br) throws XbbException {
        XbbResponse<BaseVO> response;
        if (!proBaseConfig.getXbbCode().equals(fixDataJobDTO.getXbbCode())) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
        }
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                workflowDataUpgradeHelp.upgradeWorkflowData(fixDataJobDTO);
                response = new XbbResponse<>();
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 还原工作流迁移数据
     * @param fixDataJobDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/workflowDataBack", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String workflowDataBack(@RequestBody @Valid FixDataJobDTO fixDataJobDTO, BindingResult br) throws XbbException {
        XbbResponse<BaseVO> response;
        if (!proBaseConfig.getXbbCode().equals(fixDataJobDTO.getXbbCode())) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
        }
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                workflowDataUpgradeHelp.upgradeWorkflowDataBack(fixDataJobDTO);
                response = new XbbResponse<>();
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 删除工作流迁移数据
     * @param fixDataJobDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/workflowDataDelete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String workflowDataDelete(@RequestBody @Valid FixDataJobDTO fixDataJobDTO, BindingResult br) throws XbbException {
        XbbResponse<BaseVO> response;
        if (!proBaseConfig.getXbbCode().equals(fixDataJobDTO.getXbbCode())) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
        }
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                workflowDataUpgradeHelp.workflowDataDelete(fixDataJobDTO);
                response = new XbbResponse<>();
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    @RequestMapping(value = "/openWorkflow", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String openWorkflow(@RequestBody @Valid FixDataJobDTO fixDataJobDTO, BindingResult br) throws XbbException{
        XbbResponse<BaseVO> response;
        if (!proBaseConfig.getXbbCode().equals(fixDataJobDTO.getXbbCode())) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
        }
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                String corpid = fixDataJobDTO.getCorpid();
                String type = fixDataJobDTO.getType().toString();
                CompanyConfigEntity configModelByConfigAlias = companyConfigModel.getByConfigAlias(CompanyConfigEnum.WORKFLOW.getAlias(), corpid);
                configModelByConfigAlias.setConfigValue(type);
                companyConfigModel.update(configModelByConfigAlias);
                paasRedisHelper.setValue(CompanyConfigEnum.WORKFLOW.getAlias(), corpid, type, RedisTimeConstant.TEN_MINUTES);
                response = new XbbResponse<>();
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * Description: 审批评论迁移
     * @param fixDataJobDTO
	 * @param br
     * @return java.lang.String
     * @throws
     * @author 魏荣杰
     * @date 2021/12/1 09:51
     * @since
     */
    @RequestMapping(value = "/workflowComment", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String workflowComment(@RequestBody @Valid FixDataJobDTO fixDataJobDTO, BindingResult br) throws XbbException{
        XbbResponse<BaseVO> response;
        if (!proBaseConfig.getXbbCode().equals(fixDataJobDTO.getXbbCode())) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
        }
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                workflowDataUpgradeHelp.workflowComment(fixDataJobDTO);
                response = new XbbResponse<>();
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

}
